package Sort;

import java.util.PriorityQueue;

/**
 * @author fly
 * @create 2021--03--30--8:58
 */
//topk ：小根堆
public class HeapSort {

    public static void main(String[] args) {
        int[] a=new int[]{1,2,3,4,5};
        int  k=2;
        int[] ans=helper(a,k);
        for(int x:ans){
            System.out.print(x+" ");
        }

    }

    public static int[] helper(int[] a,int k){
        int n=a.length;
        PriorityQueue<Integer> q=new PriorityQueue<>();
        for(int i=0;i<n;i++){

            if(q.size()==k){
                if(q.peek()<a[i]){
                    q.poll();
                    q.add(a[i]);
                }
            }else{
                q.add(a[i]);
            }

        }
        int[] ans=new int[k];
        for(int i=0;i<k;i++){
            ans[i]=q.poll();
        }
        return ans;
    }
}
